/*
 *  Copyright 2001-2004 The Apache Software Foundation
 *
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 */
package ar.uba.fi.posgrado.economy.dao.impl;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import org.hibernate.Criteria;
import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.criterion.Expression;
import org.springframework.dao.DataAccessException;
import org.springframework.orm.hibernate3.HibernateCallback;

import ar.uba.fi.posgrado.common.dao.AbstractDAO;
import ar.uba.fi.posgrado.common.dao.DAOException;
import ar.uba.fi.posgrado.common.exception.PosgradoException;
import ar.uba.fi.posgrado.common.exception.PosgradoRuntimeException;
import ar.uba.fi.posgrado.common.model.Student;
import ar.uba.fi.posgrado.economy.dao.DebtDAO;
import ar.uba.fi.posgrado.economy.model.Benefit;
import ar.uba.fi.posgrado.economy.model.Debt;
import ar.uba.fi.posgrado.economy.model.DebtStatus;

/**
 * 
 * @author pablo.tortorella
 * 
 * @see DebtDAO
 * @see AbstractDAO
 */
public class DebtDAOImpl extends AbstractDAO<Debt> implements DebtDAO {

    /**
     * @see ar.uba.fi.posgrado.common.dao.AbstractDAO#getClazz()
     */
    @SuppressWarnings("unchecked")
	@Override
	protected Class getClazz() {
		return Debt.class;
	}

    /**
     * @see ar.uba.fi.posgrado.economy.dao.DebtDAO#findAll(ar.uba.fi.posgrado.economy.model.DebtStatus, ar.uba.fi.posgrado.common.model.Student)
     */
    @SuppressWarnings("unchecked")
    public List<Debt> findAll(final DebtStatus status, final Student student)
            throws PosgradoRuntimeException, PosgradoException {
        List response = null;
        
        try {
            response = (List)this.getHibernateTemplate().execute(
                    new HibernateCallback() {
                        public Object doInHibernate(Session session)
                                throws HibernateException, SQLException {
                            Criteria criteria = session.createCriteria(Benefit.class);
                            criteria.add(Expression.eq("debtStatus", status));
                            criteria.add(Expression.eq("student", student));
                            return criteria.list();
                        }
                    }
            );
            if (response == null) {
                response = new ArrayList<Benefit>();
            }
        } catch (DataAccessException e) {
            throw new DAOException(
                    "Excepcion al buscar deudas con Status: " + status + 
                    " y Alumno: " + student + ". Mensaje: " + e.getMessage(), e);
        }
        
        return response;
    }

    /**
     * @see ar.uba.fi.posgrado.economy.dao.DebtDAO#findAll(ar.uba.fi.posgrado.economy.model.DebtStatus)
     */
    @SuppressWarnings("unchecked")
    public List<Debt> findAll(final DebtStatus status)
            throws PosgradoRuntimeException, PosgradoException {
        List response = null;
        
        try {
            response = (List)this.getHibernateTemplate().execute(
                    new HibernateCallback() {
                        public Object doInHibernate(Session session)
                                throws HibernateException, SQLException {
                            Criteria criteria = session.createCriteria(Benefit.class);
                            criteria.add(Expression.eq("debtStatus", status));
                            return criteria.list();
                        }
                    }
            );
            if (response == null) {
                response = new ArrayList<Benefit>();
            }
        } catch (DataAccessException e) {
            throw new DAOException(
                    "Excepcion al buscar deudas con Status: " + status + 
                    ". Mensaje: " + e.getMessage(), e);
        }
        
        return response;
    }
    
    
    

}
